package com.sdvdxl.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.alibaba.fastjson.JSON;
import com.sdvdxl.util.JsonResult;

public class BaseAction implements ServletRequestAware, ServletResponseAware, ServletContextAware {
	protected HttpServletRequest request;
	protected HttpSession session;
	protected ServletContext application;
	protected HttpServletResponse response;
	protected PrintWriter out;
	
	public static String SUCCESS = "success";
	public static String ERROR = "error";
	public static String INPUT = "input";
	
	public void writeJson(JsonResult result) {
		response.setContentType("application/json;charset=utf-8");
		String json = JSON.toJSONString(result);
		out.write(json);
		out.flush();
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	@Override
	public void setServletContext(ServletContext application) {
		this.application = application;
	}
}
